/**
 * @CoypRight:lijq (c) 2016 All Rights Reserved 
 * @Title: NetWorkHelper.java
 * @Package com.jumi.hotgirl.util
 * @author lijianqun
 * @date 2016-6-22 下午2:09:15
 * @version V1.0
 */
package com.sv.jm.baselibrary.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * @ClassName: NetWorkHelper
 * @Description: 监测网络状态
 * @author lijianqun
 * @email lijq@gmail.com‍
 * @date 2016-6-22 下午2:09:15
 */
public class NetWorkHelper {
	/** 没有网络 */
	private static final int NETWORKTYPE_INVALID = 0;
	/** wap网络 */
	private static final int NETWORKTYPE_WAP = 1;
	/** 2G网络 */
	private static final int NETWORKTYPE_2G = 2;
	/** 3G和3G以上网络，或统称为快速网络 */
	private static final int NETWORKTYPE_3G = 3;
	/** 4G网络，或统称为快速网络 */
	private static final int NETWORKTYPE_4G = 4;
	/** wifi网络 */
	private static final int NETWORKTYPE_WIFI = 5;
	/** 当前网络状态 */
	private static int CurrNetWorkType;

	private static int getMobileNetWork(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		int type = telephonyManager.getNetworkType();
		switch (type) {
		case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g ~ 100 kbps
		case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g ~ 14-64 kbps
		case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g ~ 50-100 kbps
		case TelephonyManager.NETWORK_TYPE_1xRTT:// 电信2g ~ 50-100 kbps
		case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps
			return NETWORKTYPE_2G;
		case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信 3.5g ~ 600-1400 kbps
		case TelephonyManager.NETWORK_TYPE_UMTS:// 联通 3g ~ 400-7000 kbps
		case TelephonyManager.NETWORK_TYPE_EVDO_0:// 电信 3g ~ 400-1000 kbps
		case TelephonyManager.NETWORK_TYPE_HSDPA:// 联通 3.5g ~ 2-14 Mbps
		case TelephonyManager.NETWORK_TYPE_HSUPA:// 联通 3.5g ~ 1-23 Mbps
		case TelephonyManager.NETWORK_TYPE_HSPA:// 联通 3g ~ 700-1700 kbps
		case TelephonyManager.NETWORK_TYPE_EVDO_B:// 电信 3.5g ~ 5 Mbps
		case TelephonyManager.NETWORK_TYPE_EHRPD:// 电信 3g ~ 1-2 Mbps
		case TelephonyManager.NETWORK_TYPE_HSPAP:// 联通 3g ~ 10-20 Mbps
			return NETWORKTYPE_3G;
		case TelephonyManager.NETWORK_TYPE_LTE:// ~ 10+ Mbps
			return NETWORKTYPE_4G;
		case TelephonyManager.NETWORK_TYPE_UNKNOWN:// 网络无效
			return NETWORKTYPE_INVALID;
		default:
			return NETWORKTYPE_WAP;
		}
	}

	public static int getNetWorkType(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isAvailable()) {
			int type = networkInfo.getType();
			switch (type) {
			case ConnectivityManager.TYPE_MOBILE:
				String proxyHost = android.net.Proxy.getDefaultHost();
				CurrNetWorkType = TextUtils.isEmpty(proxyHost) ? getMobileNetWork(context) : NETWORKTYPE_WAP;
				break;
			case ConnectivityManager.TYPE_WIFI:
				CurrNetWorkType = NETWORKTYPE_WIFI;
				break;
			}
		} else {
			CurrNetWorkType = NETWORKTYPE_INVALID;
		}
		return CurrNetWorkType;
	}

	/**
	 * 检测网络是否连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					if (info.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}
}
